package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.DeleteException;
import com.fantastic.common.exception.sql.InsertException;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.mapper.zjs.WorkersMapper;
import com.fantastic.model.pojo.zjs.Workers;
import com.fantastic.service.zjs.WorkersService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/5/27 11:38
 * @description: 员工信息service接口实现类
 */
@Service
public class WorkersServiceImpl implements WorkersService {

    @Resource
    private WorkersMapper workersMapper;

    /**
     * 查询
     * 无条件时是查询全部的分页查询
     * 有条件时是查询条件的分页查询
     *
     * @param workers 员工信息对象
     * @return
     */
    @Override
    public List<Workers> listWorkers(Workers workers) {
        List<Workers> workersList = workersMapper.listWorkers(workers);
        if (workersList.size() != 0) {
            return workersList;
        }
        throw new SelectException(RespStatusInfo.SELECT_NULL);
    }

    /**
     * 查询单个员工信息
     *
     * @param workerId 员工id
     * @return
     */
    @Override
    public Workers getWorkers(Integer workerId) {
        if (workerId != null) {
            Workers worker = workersMapper.getWorkerId(workerId);
            if (worker != null) {
                return worker;
            }
            //抛出未查到数据异常
            throw new SelectException(RespStatusInfo.SELECT_NULL);
        }
//        抛出查询异常
        throw new SelectException(RespStatusInfo.SELECT_ERROR);
    }

    /**
     * 添加员工信息
     *
     * @param workers 员工对象
     * @return
     */
    @Override
    public Integer insertWorkers(Workers workers) {
        workersIsNull(workers);
        //设置当前时间为入职时间
        workers.setWorkerEntryDate(LocalDateTime.now());
        //设置为在职状态
        workers.setWorkerStatus(1);
        return workersMapper.insertWorkers(workers);
    }

    /**
     * 判断姓名、姓名、年龄、身份证、手机号、是否为空
     *
     * @param workers
     */
    public void workersIsNull(Workers workers) {
        if (workers.getWorkerName() != null && workers.getWorkerName() != "" &&
                workers.getWorkerGender() != null && workers.getWorkerGender() != "" &&
                workers.getWorkerAge() != null &&
                workers.getWorkerCode() != null && workers.getWorkerCode() != "" &&
                workers.getWorkerPhone() != null && workers.getWorkerPhone() != ""
        ) {
            return;
        }
        //抛出添加异常
        throw new InsertException(RespStatusInfo.INSERT_ERROR);
    }

    /**
     * 修改员工信息
     *
     * @param workers
     * @return
     */
    @Override
    public Integer updateWorkers(Workers workers) {
        return workersMapper.updateWorkers(workers);
    }

    /**
     * 删除员工信息
     *
     * @param workersId
     * @return
     */
    @Override
    public Integer deleteWorkers(Integer workersId) {
        if (workersId != null) {
            return workersMapper.deleteWorkers(workersId);
        }
//        抛出删除异常
        throw new DeleteException(RespStatusInfo.DELETE_ERROR);
    }


}
